# coding:utf-8

"""
CopyRight(c)  2017-09-15  Fraplus Corp

Author: Weilin Shen, Xiaodong.yang
Email:  xiaodong.yang@unidt.com
Functions:

"""

import pymysql

import fralog.FraLog as FLog
from globalsettings import CUR_MODE

if CUR_MODE == 'dev':
    from globalsettings_dev import *
else:
    from globalsettings_prod import *
log = FLog.Fralog().getFileLog()

config = {
    'host': MYSQL_HOST,
    'port': MYSQL_PORT,
    'user': MYSQL_USER,
    'dbname': MYSQL_DB,
    'passwd': MYSQL_PASS
}


class UrlsManager(object):
    """
    管理客户的问卷系统连接
    """

    def __init__(self):
        super(UrlsManager).__init__()

    def connect(self):
        """
        建立数据库连接
        :return: 
        """

        #
        # 此处存在字符集方面的BUG， 建立连接时，若不指定字符集，默认是为空的，最终处理使用latin,
        # 必须在连接时指定UTF8字符集， 否则中文会出现乱码
        self.connection = pymysql.connect(host=config['host'],
                                          port=config['port'],
                                          user=config['user'],
                                          password=config['passwd'],
                                          db=config['dbname'],
                                          charset='utf8')
        # self.connection.set_charset('utf8')

    class Rec:
        custom_id = ''
        custom_name = ''
        channel = ''
        url = ''
        create_date = ''
        requester = ''
        creator = ''

    def add(self, rec):
        """
        添加记录
        :param rec: 
        :return: 
        """
        if rec is None:
            return
        try:
            with self.connection.cursor() as cursor:
                #
                #
                sql = "insert into t_survey_urls(custom_name, channel, url, create_date, requester, creator) values(%s,%s,%s,%s,%s,%s)"
                cursor.execute(sql,
                               (rec.custom_name, rec.channel, rec.url, rec.create_date, rec.requester, rec.creator))
            self.connection.commit()
            log.info('数据已成功录入数据库')
        except Exception as e:
            log.error('出错了: %s' % (e))


if __name__ == '__main__':
    um = UrlsManager()
    um.connect()
    rec = UrlsManager.Rec()
    rec.custom_name = '分度'
    rec.channel = 'fendu'
    rec.url = 'http://fra.fraplus.cn/login/wxLogin?channel=fendu&openid=null&code=v28&stat=test&title=%E7%A7%91%E5%AD%A6%E7%AE%97%E5%91%BD&fromid='
    rec.create_date = '2017-11-01 15:37:00'
    rec.requester = '魏峰'
    rec.creator = '杨小东'
    um.add(rec)

    # rec.custom_name = '中国有嘻哈'
    # rec.channel = 'china-xiha'
    # rec.url = 'http://fra.fraplus.cn/login/wxLogin?channel=china-xiha&openid=null&code=v28&stat=test&title=%e5%8f%91%e7%8e%b0%e8%87%aa%e5%b7%b1&fromid='
    # rec.create_date = '2017-10-20 17:10:00'
    # rec.requester = '魏峰'
    # rec.creator = '杨小东'
    # um.add(rec)
    #
    #
    # rec.custom_name = '通用测试'
    # rec.channel = 'common-test'
    # rec.url = 'http://fra.fraplus.cn/login/wxLogin?channel=common-test&openid=null&code=v28&stat=test&title=%e5%8f%91%e7%8e%b0%e8%87%aa%e5%b7%b1&fromid='
    # rec.create_date = '2017-10-23 13:54:00'
    # rec.requester = '魏峰'
    # rec.creator = '杨小东'
    # um.add(rec)
    #
    # rec.custom_name = '玺鉴'
    # rec.channel = 'xijian-test'
    # rec.url = 'http://fra.fraplus.cn/login/wxLogin?channel=xijian-test&openid=null&code=v28&stat=test&title=%e5%8f%91%e7%8e%b0%e8%87%aa%e5%b7%b1&fromid='
    # rec.create_date = '2017-10-24 10:01:00'
    # rec.requester = '赵菲'
    # rec.creator = '杨小东'
    # um.add(rec)
    #
    # rec.custom_name = '灿福'
    # rec.channel = 'canfu'
    # rec.url = 'http://fra.fraplus.cn/lichu?channel=canfu&openid=null&code=v28&stat=test&title=%e7%81%bf%e7%a6%8f&fromid='
    # rec.create_date = '2017-10-24 17:32:00'
    # rec.requester = '魏峰'
    # rec.creator = '杨小东'
    # um.add(rec)
